<?php

require_once(WSB_MODULES_PATH . 'sherpa/forms/Registration.php');

class Sherpa_ChangePasswordController extends WSB_Xtension_Static
{

	public function init()
	{
		$this->initTranslate();
	}

	public function indexAction(Sherpa_Form_Registration $form = null)
	{
		if(!WSB_Auth_Frontend::getInstance()->hasIdentity()) {
			return;
		}
		if(is_null($form)) {
			$form = $this->_getForm();
			$feuser = WSB_Auth_Frontend::getInstance()->getIdentity();
			$form->setDefaults($feuser->toArray());
		}
		$this->assignToView('form', $form);
		$this->renderView('changepassword/index.phtml');
	}

	public function saveAction()
	{
		if(!WSB_Auth_Frontend::getInstance()->hasIdentity()) {
			return;
		}
		$request = $this->getRequest();
		$form = $this->_getForm();
		if($request->isPost() && $form->isValid($request->getPost())) {
			$feuser = WSB_Auth_Frontend::getInstance()->getIdentity();
			$feuser->password = $form->getValue('password');
			$feuser->setTable(new Member_Model_DbTable_Feuser());
			$feuser->save();
			$this->assignToView('success', true);
			$this->indexAction();
		} else {
			$this->indexAction($form);
		}
	}

	private function _getForm()
	{
		$form = new Sherpa_Form_Registration();
		$form->removeElement('firstname');
		$form->removeElement('lastname');
		$form->removeElement('email');
		$form->getElement('btnCreateAccount')->setAttrib('src', 'sherpa/img/btn-speichern.gif');
		return $form;
	}

}
